<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Listings extends MY_Controller {

    var $language;
    /**
     * Constructor
     *
     * @access	public
     */
    function Listings() {
        parent::MY_Controller();

        // Load Necessary files
        $this->load->model('listings_model', 'listings');
        $this->load->library('pagination');
        $this->load->helper('text');
        $this->load->helper('form');
        $this->load->helper('cookie');
        // Set meta tags for the whole controller (Left here as an example)
        // If you put this in a method, it will replace metas for that method only
        // If you remove this lines below, the script will fetch the default metas that are in /application/config/template.php
        // @TODO: Put this kind of explaination in the user guide
        $metas = array(
            'title' 		=> "Listings",
            'meta_description' 	=> "Linkster is a PHP script that let you manage a links directory. It is built on the CodeIgniter PHP framework and it's free and open source.",
            'meta_keywords' 	=> "PHP script, CodeIgniter, Open Source, Free Script, Link Directory"
        );

        $this->template->metas($metas);

        if($this->input->valid_ip($this->input->ip_address())==TRUE) {
            $this->_restrict_ip($this->input->ip_address());
        }

        $this->language = $this->session->userdata('ua_language');
//$this->firephp->log($language);
        $this->lang->load('listings', $this->language);
    }
    // --------------------------------------------------------------------

    /**
     * Default Controller Function
     *
     * @access	public
     */
    function index() {
        $this->categories();
    }
    /**
     * Description:
     *
     * @param type $var Description:
     * @return type Description:
     *
     */
    function _restrict_ip($IP_Address) {
        $this->load->model('ip_to_country_model');


        $pieces = explode(".", $IP_Address);
        $w = (int) $pieces[0];
        $x = (int) $pieces[1];
        $y = (int) $pieces[2];
        $z = (int) $pieces[3];


        $IP_Number = 16777216*$y + 65536*$x + 256*$y + $z;

        // $language = $this->ip_to_country_model->check_country( $IP_Number);
        //$this->firephp->log($three_digit_code);
        //  if($language == FALSE)
        //      return FALSE;

        //   else
        //       return TRUE;

    }

    // --------------------------------------------------------------------

    /**
     * Display all categories
     *
     * @access	public
     */
    function categories() {
        $data['categories'] 	= $this->listings->get_all_categories();
        $this->template->display('listings/categories', $data);
    }



    // --------------------------------------------------------------------

    /**
     * Display the requested category listings
     *
     * @access	public
     * @param	string	category name
     */
    function category($category = FALSE) {
        // Get the entries for the requested category
        $lastweek = date('y-m-d',mktime(0, 0, 0, date("m")  , date("d")-7, date("Y")) );

        $data_last_week = $this->listings->get_category_listings_from_to(intval($category), $lastweek, date("y-m-d") , $this->pagination->per_page, $this->uri->segment(4));
        $data_before_last_week = $this->listings->get_category_listings_from_to(intval($category), '',$lastweek , $this->pagination->per_page, $this->uri->segment(4));

        /*  if(( count($data_last_week['entries']) == 0)&&( count($data_before_last_week['entries']) == 0)) {
            // Category doesn't exist
            $this->session->set_flashdata('msg', 'This category does not exist.');
            redirect();
        }
        else if(($data_last_week['category']->entry_count == 0)&&($data_before_last_week['category']->entry_count == 0)) {
            // Category is empty, we show a flash message
            $this->session->set_flashdata('msg', 'This category is empty.  Be the first to '.anchor('member/listings/add/'.$category, 'add an entry').'.');
            redirect();
        }*/
        //    else {
        // Set pagination
        $config['base_url'] 	= site_url('listings/category/'.$category);
      //  $config['total_rows'] 	= $data_last_week['category']->entry_count + $data_before_last_week['category']->entry_count;
        $this->pagination->initialize($config);
       // $this->firephp->log($data_before_last_week );
        $data['pagination'] 	= $this->pagination->create_links();
        //   }
        $data = array('last_week'=>$data_last_week,'before_last_week'=>$data_before_last_week);

        //  $this->template->sidebar('listings', array('category_id' => $category));
        $this->template->display('listings/listings', $data);

    }

    // --------------------------------------------------------------------

    /**
     * Display a listing details
     *
     * @access	public
     * @param	int		listing ID (entry ID)
     */
    function details($entry_id) {
        $this->load->model('users_model', 'users');

        $data['entry'] = $this->listings->get_entry( intval($entry_id) );

        if( count($data['entry']) == 0) {
            // Entry doesn't exist
            $this->session->set_flashdata('msg', 'This entry does not exist.');
            redirect();
        }
        $data['language'] = $this->language;
        //  $data['owner'] = $this->users->get_user($data['entry']->FK_user_id);

        // $this->template->sidebar('listings', array('category_id' => $data['entry']->FK_category_id));
        $this->template->display('listings/details', $data);
    }

    // switch session's language and set a cookie
    function set_language() {

        $language = $this->uri->segment(3);
        if ( empty($language) ) {
            $language = $this->input->post('lang_select');
        }
        if ( $language == 'detect' ) {
            // kill the cookie & session variable
            set_cookie('ua_language', '');
            $this->session->set_userdata('ua_language', '');
        } else {
            // switch session's language and set a cookie
            set_cookie('ua_language', $language);
            $this->session->set_userdata('ua_language', $language);
        }
        redirect ($this->session->flashdata('uri'));
        //redirect('../front');
    }
}


/* End of file listings.php */
/* Location: ./application/controllers/listings.php */